package com.lewis.module.m2.api.tiktok.utils;

import org.jsoup.Connection;
import org.jsoup.Jsoup;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 爬虫工具类
 *
 * @author Lewis 2021年09月22日 11:21
 */
public class SpiderUtil {

	/**
	 * 请求链接URL.
	 *
	 * @param url 被链接URL
	 * @return 响应
	 */
	public static Connection.Response requireUrl(String url) {
		Connection.Response execute = null;
		try {
			execute = Jsoup.connect(url)
					.userAgent("Dalvik/2.1.0 (Linux; U; Android 11)")
					.ignoreContentType(true)
					.followRedirects(true)
					.execute();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return execute;
	}

	/**
	 * 正则提取字符串
	 *
	 * @param str   被提取的字符串
	 * @param regex 提取规则
	 * @return 提取的字符
	 */
	public static String regexMatch(String str, String regex) {
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(str);
		String subStr = null;
		while (matcher.find()) {
			subStr = matcher.group(1).trim();
		}
		return subStr;
	}
}
